<template>
  <div class="page">
    <div class="page-header">
      <el-button @click="goBack" icon="ArrowLeft">返回</el-button>
      <h2 class="page-title">我的评价</h2>
    </div>

    <!-- 评价列表 -->
    <div v-if="reviews.length === 0" class="no-reviews">
      <div class="no-reviews-icon">📝</div>
      <div class="no-reviews-text">您还没有写过评价</div>
    </div>

    <div v-else class="reviews-list">
      <div 
        v-for="review in reviews" 
        :key="review.id" 
        class="review-item"
      >
        <div class="review-header">
          <div class="product-info">
            <h3 class="product-name">{{ review.product?.name || '商品' }}</h3>
            <div class="review-date">{{ formatDate(review.createdAt) }}</div>
          </div>
          <div class="review-rating">
            <span 
              v-for="i in 5" 
              :key="i" 
              class="star"
              :class="{ filled: i <= review.rating }"
            >
              ★
            </span>
          </div>
        </div>
        <div class="review-content">{{ review.comment }}</div>
        
        <div class="review-actions">
          <el-button
            type="danger"
            size="small"
            :loading="deleting"
            @click="deleteReview(review.id)"
          >
            {{ deleting ? '删除中...' : '删除评价' }}
          </el-button>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <div v-if="totalPages > 1" class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        :page-size="pageSize"
        :total="total"
        layout="prev, pager, next"
        @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { getUserReviews, deleteReview as deleteReviewApi } from '@/api/review'
import { showFullscreenLoading } from '@/utils/loading'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

// 响应式数据
const reviews = ref([])
const deleting = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const totalPages = ref(0)
const total = ref(0)

// 方法
const loadReviews = async () => {
  if (!userStore.user) {
    ElMessage.warning('请先登录')
    router.push('/login')
    return
  }

  const loading = showFullscreenLoading({
    text: '正在加载评价...'
  })

  try {
    const response = await getUserReviews(userStore.user.id, {
      page: currentPage.value - 1,
      size: pageSize.value
    })
    
    if (response.data.success) {
      reviews.value = response.data.data.content || []
      total.value = response.data.data.totalElements || 0
      totalPages.value = response.data.data.totalPages || 0
    }
  } catch (error: any) {
    console.error('加载评价失败:', error)
    ElMessage.error(error.response?.data?.message || '加载评价失败')
  } finally {
    loading.close()
  }
}

const deleteReview = async (reviewId: number) => {
  if (!userStore.user || !confirm('确定要删除这条评价吗？')) return

  try {
    deleting.value = true
    const response = await deleteReviewApi(reviewId, userStore.user.id)
    if (response.data.success) {
      await loadReviews()
      ElMessage.success('评价删除成功！')
    }
  } catch (error: any) {
    console.error('删除评价失败:', error)
    ElMessage.error(error.response?.data?.message || '删除评价失败，请重试')
  } finally {
    deleting.value = false
  }
}

const handlePageChange = (page: number) => {
  currentPage.value = page
  loadReviews()
}

const formatDate = (dateString: string) => {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN')
}

const goBack = () => {
  router.back()
}

onMounted(() => {
  loadReviews()
})
</script>

<style scoped>
.page {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.back-btn {
  background: none;
  border: none;
  color: #007aff;
  font-size: 16px;
  cursor: pointer;
  padding: 10px 0;
  margin-right: 15px;
}

.page-title {
  margin: 0;
  font-size: 24px;
  color: #333;
}

.loading {
  text-align: center;
  padding: 60px 20px;
  color: #666;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f0f0f0;
  border-top: 4px solid #ff6b35;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 15px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.no-reviews {
  text-align: center;
  padding: 60px 20px;
  color: #666;
}

.no-reviews-icon {
  font-size: 64px;
  margin-bottom: 20px;
}

.no-reviews-text {
  font-size: 18px;
  margin-bottom: 30px;
}

.reviews-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.review-item {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.product-info {
  display: flex;
  gap: 15px;
  margin-bottom: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.product-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 8px;
}

.product-details {
  flex: 1;
}

.product-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin: 0 0 8px 0;
}

.product-price {
  font-size: 18px;
  font-weight: bold;
  color: #ff6b35;
}

.review-content {
  margin-bottom: 15px;
}

.review-rating {
  margin-bottom: 10px;
}

.star {
  color: #ddd;
  font-size: 18px;
}

.star.filled {
  color: #ff6b35;
}

.review-text {
  color: #666;
  line-height: 1.6;
  margin-bottom: 10px;
}

.review-date {
  font-size: 12px;
  color: #999;
}

.review-actions {
  text-align: right;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
  margin-top: 30px;
}

.page-btn {
  padding: 8px 16px;
  border: 1px solid #ddd;
  background: #fff;
  border-radius: 4px;
  cursor: pointer;
}

.page-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  color: #666;
  font-size: 14px;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.btn-primary {
  background: #ff6b35;
  color: #fff;
}

.btn-primary:hover {
  background: #e55a2b;
}

.btn-danger {
  background: #ff3b30;
  color: #fff;
}

.btn-danger:hover {
  background: #e02e24;
}
</style> 